package ProjectTest;

import java.awt.Container;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;

public class TreeWin extends JFrame implements MouseListener{
	JTree tree;

	TreeWin() {
		setSize(300, 250);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container con = getContentPane();
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("根结点");
		DefaultMutableTreeNode t1 = new DefaultMutableTreeNode("结点");
		DefaultMutableTreeNode t2 = new DefaultMutableTreeNode("结点");
		DefaultMutableTreeNode t1_1 = new DefaultMutableTreeNode("叶结点");
		DefaultMutableTreeNode t1_2 = new DefaultMutableTreeNode("叶结点");
		root.add(t1);
		root.add(t2);
		t1.add(t1_1);
		t2.add(t1_2);
		tree = new JTree(root); // 创建根为root的树
//		tree.addMouseListener(this);
		tree.addMouseListener((MouseListener) this);
		JScrollPane scrollPane = new JScrollPane(tree);
		con.add(scrollPane);
	}

	public static void main(String[] args) {
		TreeWin win = new TreeWin();
		win.setVisible(true);
	}

	
	public void mouseClicked(MouseEvent me) {
		  DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
		  if(me.getClickCount()==2){ //鼠标双击事件
		   if(selectionNode.isLeaf()){ //叶子节点的监听
		    System.out.println(selectionNode.toString());
		   }
		  }
		 }

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
//	public void valueChanged(TreeSelectionEvent tse) {
//		DefaultMutableTreeNode selectionNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
//		if (selectionNode.isLeaf()) { // 叶子节点的监听
//			System.out.println(selectionNode.toString());
//		}
//	}
}